package rewardsonline.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.WebContentInterceptor;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;

import common.datetime.SimpleDateFormatter;
import common.money.MonetaryAmountFormatter;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

	@Override
    public Validator getValidator() {
        return new LocalValidatorFactoryBean();
    }
	
	/**
	 * TODO-02: Add configuration for a ThemeChangeInterceptor. Try
	 * switching to Light now. The look doesn't change
	 * because we need a new theme-resolver.
	 */
	
	/**
	 * TODO-03: Add configuration for a CookieThemeResolver. The look will be the same. 
	 * because we have set the theme, but aren't using it.  
	 * Open the URL in Firefox and, under
	 * the Tools menu, select Web-Developer Extensions -> Cookies ->
	 * View Cookie Information.  This cookie should have the value light:
	 * org.springframework.web.servlet.theme.CookieThemeResolver.THEME
	 */
	
	/**
	 * TODO-06: Do the same for locale. Click on 'Français'. Again nothing
	 * happens. Note the URL. Add configuration for a
	 * LocalChangeInterceptor.
	 */
	
	/**
	 * TODO-07: Add configuration for a CookieLocaleResolver. Now you should
	 * be able to change everything to French, and back to English.
	 */
	

	@Bean 
	@Description("Interceptor for caching response headers.")
	WebContentInterceptor webContentInterceptor() {
		WebContentInterceptor wci = new WebContentInterceptor();
		wci.setCacheSeconds(0);
		return wci;
	}
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(webContentInterceptor());
    }
	
	/**
	 * Define controllers which only render pages (i.e. nothing added to the 
	 * model)
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("welcome");
		registry.addViewController("/home").setViewName("welcome");
	}
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		// Resolves view names to Tiles definitions
		registry.tiles();
	}

	@Bean
	ViewResolver tilesViewResolver() {
		TilesViewResolver tilesViewResolver = new TilesViewResolver();
		tilesViewResolver.setRequestContextAttribute("requestContext");
		return tilesViewResolver;
	}

	@Bean 
	@Description("Configures Tiles")
	TilesConfigurer tilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();
		tilesConfigurer.setDefinitions("/WEB-INF/tiles.xml");
		return tilesConfigurer;
	}
	
	/**
	 * Define a custom ConversionService
	 */
	@Override
	public void addFormatters(FormatterRegistry registry) {
		registry.addFormatter(new MonetaryAmountFormatter());
		registry.addFormatter(new SimpleDateFormatter());
	}

	@Bean
	MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:messages");
		return messageSource;
	}

}
